<?php
/**
 * Bdat Site development
 * @author Will Skates
 */

/*----------------------------------------------------------------------------------------------------*/

/**
 * Masonry Factory
 * This class gets the data for each masonry block and puts them in the right order before telling
 * the view to render the html for them.
 * @author Will Skates
 */

class masonryfactory extends CI_Controller {
	
	protected $blocks 		= array();
	protected $stickies	 	= array();
	
	protected $blockTypes 	= array(
		'NewsItem','CourseInfo','Event','Resource','Showcase','Tweet'
	);
	
	/**
	 * This function get runs when the site homepage is loaded.
	 * @return Void
	 */
	public function index()
	{
		
		//Load up the page header and html that starts off the logo block.
		$this->load->View('header');
		$this->load->View('LogoBlock');
		
		$result = array();
		
		//Go through each type of block and get all of the blocks for that type.
		foreach ($this->blockTypes as $type) {
			$this->load->model($type,'',true);
			$result[$type] = $this->$type->getAll();
		}
		
		$ci = array();
		$re = array();
		
		//Go through all of the block types and put them in the right array for their category.
		foreach ($result as $key=>$value) {
			
			$len = sizeof($value);
			$i=0;
			while ( $i < $len ) {
				
				$value[$i]->type = $key;
				if ( $value[$i]->sticky == '1' && $key != 'CourseInfo' && $key != 'Resource' ) {
					$this->stickies[] = $value[$i];
				} else if ($key == 'CourseInfo') {
					$ci[] = $value[$i];
				} else if ($key == 'Resource') {
					$re[] = $value[$i];
				} else {
					$this->blocks[] = $value[$i];
				}
				$i++;
				
			}
			
		}
		
		//Randomize the blocks.
		shuffle( $this->stickies );
		shuffle( $this->blocks );
		
		//Place all course info and resource blocks ath the top of the list.
		$this->stickies = array_merge( $ci, $re, $this->stickies );
		
		//Merge all of the arrays together to form a complete list with them all in the right order.
		$this->blocks = array_merge( $this->stickies, $this->blocks );
		
		//Get rid of any data that we no longer need.
		unset($result,$this->stickies);
		
		//Load the html view with the block data.
		$this->load->view('MasonryBlocks',array('blocks'=>$this->blocks));
		$this->load->view('loginBlock');
		$this->load->View('footer');
		
	}
	
	
	
	
	
}